using UnityEngine;
using UnityEngine.EventSystems;

namespace Gj
{
    public class ClickHelper : NodeHelper, IPointerDownHandler, IPointerUpHandler
    {
        public AudioClip clip;
        public SoundType soundType;
        public NodeHelper[] list;
        public bool invert;
        public bool click;
        private bool _allow;
        private bool d;
        private float endTime;
        private float startTime;
        private bool drag;

        protected override BindDataType bindDataType => BindDataType.Bool;

        private void Update()
        {
            if (drag) Emit(UIEvent.Drag);
            if (startTime == 0) return;
            if (endTime == 0 && startTime > 0 && Time.time - startTime > SystemInput.loopTime)
            {
                endTime = 0;
                startTime = 0;
                d = false;
                Emit(UIEvent.LongPress);
                return;
            }

            if (endTime > 0 && startTime > 0)
            {
                if (endTime > startTime)
                {
                    // 判断单击双击
                    if (endTime - startTime > SystemInput.clickTime)
                    {
                        // 超过点击时间取消
                        endTime = 0;
                        startTime = 0;
                        d = false;
                    }
                    else if (Time.time - endTime > SystemInput.waitTime || click)
                    {
                        // 超过等待双击时间 则确定为单击
                        endTime = 0;
                        startTime = 0;
                        d = false;
                        Emit(UIEvent.Click);

                        if (soundType != SoundType.None)
                            Game.single.PlayEFF(soundType);
                        else if (clip != null) Game.single.PlayEFF(clip);
                    }
                    else if (endTime - startTime < SystemInput.clickTime)
                    {
                        if (d)
                        {
                            // 如果是第二次点击 直接判断为双击
                            endTime = 0;
                            startTime = 0;
                            d = false;
                            Emit(UIEvent.DoubleClick);

                            if (soundType != SoundType.None)
                                Game.single.PlayEFF(soundType);
                            else if (clip != null) Game.single.PlayEFF(clip);
                        }
                    }
                }
                else if (startTime > endTime)
                {
                    //判断是否符合第二次点击
                    if (startTime - endTime < SystemInput.waitTime) d = true;
                }
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            startTime = Time.time;
            Emit(UIEvent.Down);
            drag = true;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (!drag) return;
            drag = false;
            endTime = Time.time;
            Emit(UIEvent.Up);
            Update();
        }

        protected override void BindData(bool b)
        {
            _allow = invert ? !b : b;
            for (var i = 0; i < list.Length; i++) list[i].SetData(_allow);
        }
    }
}